//
//  JHBaseINavigationController.swift
//  JHSinaWeibo
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class JHBaseINavigationController: UINavigationController,UIGestureRecognizerDelegate {
    override func viewDidLoad(){
    super.viewDidLoad()
    //解决右滑返回手势的问题
    //存在bug在最顶层的页面 右滑返回手势会响应
        self.interactivePopGestureRecognizer?.delegate = self
    }
    //重写push方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //判断是否是顶层界面
        var backTitle = "返回"
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1 {
                //设置返回按钮的title = 顶级界面的title
                backTitle = childViewControllers.first?.title ?? "返回"
            }
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext",title: backTitle,target: self,action: #selector(popToFrontVC))
            viewController.navigationItem.leftBarButtonItem = back
        }
        super.pushViewController(viewController, animated: animated)
        print(childViewControllers.count)
    }
    @objc private func popToFrontVC(){
        
    popViewControllerAnimated(true)
        
    }
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count > 1
    }
}
